package template.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.log4j.Logger;

/**
 * 自定义BeanUtils
 * 主要是用于在局部使用自定义转化时间类型的转换器
 * 
 * @author mengyang
 * @version 1.0
 */
public class BeanUtils {
	
	private static final Logger logger = Logger.getLogger(BeanUtils.class);
	
	private static final DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
	private static final DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static final DateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static BeanUtilsBean beanUtilsBean;
	public static ConvertUtilsBean convertUtilsBean;
	
	static{
		convertUtilsBean = new ConvertUtilsBean();
		convertUtilsBean.register(new Converter(){

			public Object convert(Class arg0, Object arg1) {
				
				String p = (String)arg1;

				if(p== null || p.trim().length()==0){
					return null;
				}
				p = p.trim();
				try{
					if(p.length() == 10){
						return df1.parse(p);
					}else if(p.length() == 16){
						return df2.parse(p);
					}else if(p.length() == 19){
						return df3.parse(p);
					}else{
						return null;
					}
				}catch(Exception e){
					logger.error("把"+arg1+"转换成时间类型出错", e);
					return null;
				}
				
			}
			
		}, Date.class);
		
		/*convertUtilsBean.register(new Converter(){

			public Object convert(Class arg0, Object arg1) {
				String str = (String) arg1;
				if(StringUtils.isNotBlank(str)){
					return Integer.valueOf(str);
				}
				return null;
			}
			
		}, Integer.class);*/
		
		beanUtilsBean = new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());
	}

	public static void main(String[] args){

		System.out.println(Integer.valueOf(""));
	}
	
	
}
